﻿#include "SelectionManager.h"

SelectionManager::SelectionManager()
{
    m_selectedActors = vtkSmartPointer<vtkActorCollection>::New();

    // 创建高亮属性
    m_selectedProperty = vtkSmartPointer<vtkProperty>::New();
    m_selectedProperty->SetColor(1.0, 1, 0.0); // 紫色
    m_selectedProperty->SetLineWidth(6.0);
    m_selectedProperty->SetPointSize(10.0);
}

void SelectionManager::AddSelectedActor(vtkActor* actor, PickMode mode)
{
    if(!actor) return;

    // 保存原始属性
    SelectedActorInfo info;
    info.originalProperty = vtkSmartPointer<vtkProperty>::New();
    info.originalProperty->DeepCopy(actor->GetProperty());
    info.mode = mode;
    m_actorOrigInfoMap[actor] = info;

    // 应用高亮属性
    SetHighlight(actor, mode);

    m_selectedActors->AddItem(actor);
}
void SelectionManager::RemoveSeletedActor(vtkActor* actor)
{
    if(!actor || m_actorOrigInfoMap.find(actor) == m_actorOrigInfoMap.end()) return;

    // 恢复原始属性
    RestoreOriginalProperty(actor);
    ResetMapperOffset(actor);

    m_selectedActors->RemoveItem(actor);
    m_actorOrigInfoMap.erase(actor);
}

void SelectionManager::ClearAllSelectedActors()
{
    for(auto& pair : m_actorOrigInfoMap)
    {
        RestoreOriginalProperty(pair.first);
    }
    m_selectedActors->RemoveAllItems();
    m_actorOrigInfoMap.clear();
}

void SelectionManager::SetHighlightColor(double r, double g, double b)
{
    m_selectedProperty->SetColor(r, g, b);
    UpdateAllHighlights();
}


void SelectionManager::SetMapperOffset(vtkActor* actor, double offset)
{
    if(!actor) return;

    vtkMapper* mapper = actor->GetMapper();
    if(mapper)
    {
        mapper->SetResolveCoincidentTopologyToPolygonOffset();
        mapper->SetResolveCoincidentTopologyPolygonOffsetParameters(offset, offset);
    }
}

void SelectionManager::ResetMapperOffset(vtkActor* actor)
{
    SetMapperOffset(actor, 0);
}
vtkActorCollection* SelectionManager::GetSelectedActors() const
{
    return m_selectedActors;
}

std::vector<vtkActor*> SelectionManager::GetSelectedActors()
{
    if(!m_selectedActors)
    {
        return std::vector<vtkActor*>();
    }

    m_selectedActors->InitTraversal();
    std::vector<vtkActor*> actors;
    vtkActor* actor = m_selectedActors->GetNextItem();
    while(actor)
    {
        actors.push_back(actor);
        actor = m_selectedActors->GetNextItem();
    }

    return actors;
}

bool SelectionManager::IsActorHighlighted(vtkActor* actor) const
{
    return m_selectedActors->IsItemPresent(actor);
}

void SelectionManager::SetHighlight(vtkActor* actor, PickMode mode)
{
    switch(mode)
    {
    case PickMode::VOLUME:
        actor->SetProperty(m_selectedProperty);
        break;

    case PickMode::FACE:
        actor->SetProperty(m_selectedProperty);
        actor->GetProperty()->SetRepresentationToSurface();
        break;

    case PickMode::EDGE:
        actor->SetProperty(m_selectedProperty);
        actor->GetProperty()->SetRepresentationToWireframe();
        break;

    case PickMode::POINT:
        actor->SetProperty(m_selectedProperty);
        actor->GetProperty()->SetRepresentationToPoints();
        break;
    }

    // 应用Mapper偏移避免深度冲突
    SetMapperOffset(actor, 1.0);
}

void SelectionManager::RestoreOriginalProperty(vtkActor* actor)
{
    if(m_actorOrigInfoMap.find(actor) != m_actorOrigInfoMap.end())
    {
        actor->SetProperty(m_actorOrigInfoMap[actor].originalProperty);
        ResetMapperOffset(actor);
    }
}

void SelectionManager::UpdateAllHighlights()
{
    for(auto& pair : m_actorOrigInfoMap)
    {
        SetHighlight(pair.first, pair.second.mode);
    }
}
